package com.hee.erp.modules.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hee.erp.annotation.Log;
import com.hee.erp.common.R;
import com.hee.erp.constant.Const;
import com.hee.erp.filter.LoginContextHolder;
import com.hee.erp.modules.entity.SysUser;
import com.hee.erp.modules.service.ISysUserService;
import com.hee.erp.pojo.LoginInfo;
import com.hee.erp.util.JwtUtil;
import com.hee.erp.vo.LoginResultVo;
import com.hee.erp.vo.SysUserLoginVo;
import com.hee.erp.vo.UserInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/modules/sysUser2")
@Api(tags = "sysUser2",produces = "application/json")
@Slf4j
public class SysUser2Controller {

    @Autowired
    ISysUserService sysUserService;

    @PostMapping("/login")
    @ApiOperation(value = "login")
    @Log("登录")
    public R login(@RequestBody SysUserLoginVo vo, HttpServletRequest request, HttpServletResponse response) throws Exception {
        SysUser user = sysUserService.login(vo);
        String token = JwtUtil.createToken(user.getId(), user.getDbKey());
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setToken(token);
        loginResultVo.setUserId(user.getId());

        LoginResultVo.RoleInfo roleInfo = new LoginResultVo.RoleInfo();
        roleInfo.setRoleName("管理员");
        roleInfo.setValue("admin");
        loginResultVo.setRoleInfo(roleInfo);

        //保存登录信息到本地线程
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUserId(user.getId());
        loginInfo.setUserDbKey(user.getDbKey());
        loginInfo.setExpireTimeMillis(System.currentTimeMillis() + JwtUtil.EXPIRATION);
        loginInfo.setRequest(request);
        LoginContextHolder.set(loginInfo);

        return R.success(loginResultVo);
    }

    @GetMapping("/getUserInfo")
    @ApiOperation(value = "获取用户信息")
    @Log("获取用户信息")
    public R getUserInfo() {
        LoginInfo loginInfo = LoginContextHolder.get();
        SysUser user = sysUserService.getById(loginInfo.getUserId());

        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setUserId(user.getId());
        userInfoVo.setUsername(user.getUserName());
        userInfoVo.setRealName(user.getUserName());
        userInfoVo.setAvatar(user.getIdentificationPhoto());
        userInfoVo.setDesc(user.getRemark());

        return R.success(userInfoVo);

    }

    @GetMapping("/getPermCode")
    public R getPermCode(){
        JSONObject jsonObject = new JSONObject();
        JSONArray btnPermCode1 = new JSONArray();
        btnPermCode1.add("1000");
        btnPermCode1.add("3000");
        btnPermCode1.add("5000");
        jsonObject.put("1", btnPermCode1);

        JSONArray btnPermCode2 = new JSONArray();
        btnPermCode2.add("2000");
        btnPermCode2.add("4000");
        btnPermCode2.add("6000");
        jsonObject.put("2", btnPermCode2);

        return R.success(jsonObject);
    }
}
